package icraf.carbon.gui;

import icraf.common.gui.MenuAdapter;
import icraf.common.gui.MenuGenerator;

import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;

public class MenuFactory extends MenuGenerator {

	public enum MenuProperty implements MenuAdapter {
		FILE("FileMenu.file"), FILE_EXIT("FileMenu.exit"), FILE_NEW(
				"FileMenu.new"), FILE_OPEN("FileMenu.open"), 
				FILE_SAVE("FileMenu.save"), FILE_SAVE_AS("FileMenu.save_as"),
				FILE_SAVE_OUTPUT("FileMenu.save_output"),

		APP_TITLE("Application.title"),
		
		APP_EXIT("Application.exit"), 
		APP_SET("Application.set_project"), 
		APP_SAVE("Application.save_project"), 
		APP_SAVE_ALL("Application.save_all_project"), 
		APP_CLOSE("Application.close_project"),
		APP_HELP("Application.help_error"),
		APP_FILE("Application.file_error"), 
		APP_WRITE("Application.write_error"), 
		APP_MANUAL("Application.manual"),
		APP_HELP_OPEN("Application.help_open"),

		
		INPUT_LABEL("InputMenu.label"),
		INPUT_DESC("InputMenu.description"),
		INPUT_TIME("InputMenu.time"),
		
		INPUT_INCLUDE_BG("InputMenu.include_bg"),
		INPUT_INCLUDE_MODIF("InputMenu.include_modif"),
		INPUT_EMISSION_FACTOR("InputMenu.emission_factor"),
		INPUT_ACTUAL_EMISSION("InputMenu.actual_emission"),
		INPUT_CLIPBOARD_MATRIX("InputMenu.clipboard_matrix"),
		INPUT_CLIPBOARD_HEADER("InputMenu.clipboard_header"),
		INPUT_CLIPBOARD_HEADER_INDEX("InputMenu.clipboard_header_index"),
		INPUT_IMPORT("InputMenu.import"),
		INPUT_CANCEL("InputMenu.cancel"),
		INPUT_RESET("InputMenu.reset"),
		INPUT_RESET_CONFIRM("InputMenu.reset_confirm"),
		
		IMAGE_SAVE("ImageMenu.save"),IMAGE_COPY("ImageMenu.copy"),
		
		CHART_ZOOM ("ChartMenu.zoom"),
		CHART_ZOOM_IN ("ChartMenu.zoom_in"),
		CHART_ZOOM_OUT ("ChartMenu.zoom_out"),
		CHART_ZOOM_SIZE ("ChartMenu.zoom_size"),
		CHART_ZOOM_WIDTH ("ChartMenu.zoom_width"),
		CHART_ZOOM_HEIGHT ("ChartMenu.zoom_height"),
		CHART_ZOOM_FIT ("ChartMenu.zoom_fit"),
		
		CHART_YSCALE ("ChartMenu.chart_yscale"),
		CHART_YSCALE_NORMAL ("ChartMenu.chart_yscale_normal"),
		CHART_YSCALE_LOG ("ChartMenu.chart_yscale_log"),
		CHART_XSCALE ("ChartMenu.chart_xscale"),
		CHART_XSCALE_HA ("ChartMenu.chart_xscale_ha"),
		CHART_XSCALE_TOTAL ("ChartMenu.chart_xscale_total"),
		
		CHART_SHOW_EMISSION("ChartMenu.show_emission"),
		CHART_SHOW_SEQUESTRATION("ChartMenu.show_sequestration"),
		CHART_SHOW_POSITIVE_NPV("ChartMenu.show_positive_npv"),
		CHART_SHOW_NEGATIVE_NPV("ChartMenu.show_negative_npv"),
		CHART_SHOW_THRESHOLD("ChartMenu.show_threshold"),
		CHART_HIDE_THRESHOLD("ChartMenu.hide_threshold"),
		CHART_SHOW_LEGEND("ChartMenu.show_legend"),
		
		CHART_FORMAT ("ChartMenu.format_chart"),
		CHART_FORMAT_X_AXIS ("ChartMenu.format_axis_x"),
		CHART_FORMAT_Y_AXIS ("ChartMenu.format_axis_y"),
		CHART_FORMAT_BAR ("ChartMenu.format_bar"),
		CHART_FORMAT_LABEL ("ChartMenu.format_label"),
		CHART_FORMAT_THRESHOLD ("ChartMenu.format_threshold"),
		CHART_FORMAT_LEGEND ("ChartMenu.format_legend"),
		CHART_FORMAT_BACKGROUND ("ChartMenu.format_background"),
		
		CHART_AXIS_MAIN ("ChartMenu.axis_main"),
		CHART_AXIS_UNIT ("ChartMenu.axis_unit"),
		CHART_LINE ("ChartMenu.line"),
		CHART_FONT ("ChartMenu.font"),
		CHART_SHAPE ("ChartMenu.shape"),
		
		CHART_SETTING_LINE_SIZE ("ChartMenu.setting_line_size"),
		CHART_SETTING_LINE_TYPE ("ChartMenu.setting_line_type"),
		CHART_SETTING_LINE_COLOR ("ChartMenu.setting_line_color"),
		CHART_SETTING_FILL_COLOR ("ChartMenu.setting_fill_color"),
		CHART_SETTING_CORNER ("ChartMenu.setting_corner"),
		CHART_SETTING_PREVIEW ("ChartMenu.setting_preview"),
		CHART_SETTING_COLOR_TEMPLATE ("ChartMenu.setting_color_template"),
		CHART_SETTING_DISPLAY ("ChartMenu.setting_display"),
		
		CHART_LABEL_ADD("ChartMenu.label_add"),CHART_LABEL_REMOVE("ChartMenu.label_remove"),
		
		CHART_LEGEND_COLOR ("ChartMenu.legend_color"),
		CHART_LEGEND_COLOR_THEME ("ChartMenu.legend_color_theme"),
		CHART_THRESHOLD ("ChartMenu.threshold"),
		CHART_THRESHOLD_LINE_EMISSION ("ChartMenu.threshold_line_emission"),
		CHART_THRESHOLD_LINE_COST ("ChartMenu.threshold_line_cost"),
		
		CHART_ELI_ONLY ("ChartMenu.eligible_only"),
		CHART_AXIS_SETTING ("ChartMenu.axis_setting"),
		
		EDIT_COPY("EditMenu.copy"),EDIT_PASTE("EditMenu.paste"),EDIT_COPY_TABLE("EditMenu.copy_table"),
		EDIT_DELETE("EditMenu.delete"),EDIT_UPDATE("EditMenu.update"),
		EDIT_DETAIL("EditMenu.detail"),
		EDIT_COPY_ERROR("EditMenu.copy_error"),
		EDIT_SELECT_ERROR("EditMenu.select_error"),
		
		GENERAL_ABOUT("GeneralMenu.about"), 
		GENERAL_HELP("GeneralMenu.help"),
		GENERAL_BACK("GeneralMenu.back"),
		GENERAL_NEXT("GeneralMenu.next"),
		GENERAL_PREVIOUS("GeneralMenu.previous"),
		GENERAL_FIRST("GeneralMenu.first"),
		GENERAL_LAST("GeneralMenu.last"),
		GENERAL_FULLSCREEN("GeneralMenu.fullscreen"),
		GENERAL_PRINT("GeneralMenu.print"),
		GENERAL_VIEW_MATRIX("GeneralMenu.view_matrix"),
		GENERAL_VIEW_FORM("GeneralMenu.view_form"),
		GENERAL_TRANSFORM("GeneralMenu.transform"),
		GENERAL_TRANSFORM_MATRIX("GeneralMenu.transform_matrix"),
		GENERAL_TRANSFORM_WARNING("GeneralMenu.transform_warning"),

		LIST_ADD("ListMenu.add"), 
		LIST_LOAD("ListMenu.load"), 
		LIST_REMOVE("ListMenu.remove"), 
		LIST_SAVE("ListMenu.save"),
		LIST_IMPORT_CLIPBOARD("ListMenu.import_clipboard"),
		
		//Project Menu
		
		PROJECT("ProjectMenu.project"),
		PROJECT_MANAGER("ProjectMenu.project_manager"), 
		PROJECT_CLOSE("ProjectMenu.close"), 
		PROJECT_CSTOCK("ProjectMenu.cstock"),
		
		PROJECT_VARIABLE("ProjectMenu.variable"),
		PROJECT_VALUE("ProjectMenu.value"),
		PROJECT_FORMULA("ProjectMenu.formula"),
		PROJECT_FIELDS("ProjectMenu.fields"),
		PROJECT_TOTAL("ProjectMenu.total"),
				
		//****** INPUT
		PROJECT_INPUT("ProjectMenu.input"),
		//******
		PROJECT_IMPORT_RAW("ProjectMenu.import_raw"),
		PROJECT_IMPORT_INITIAL("ProjectMenu.import_initial_lu"),
		PROJECT_IMPORT_SUBSEQUENT("ProjectMenu.import_subsequent_lu"),
		PROJECT_IMPORT_WARNING("ProjectMenu.import_warning"),
		PROJECT_IMPORT_DONE("ProjectMenu.import_done"),
		PROJECT_IMPORT_SETTING("ProjectMenu.import_setting"),
		PROJECT_IMPORT_SELECT("ProjectMenu.import_select"),
		PROJECT_IMPORT_PROGRESS("ProjectMenu.import_progress"),
		
		//******
		PROJECT_LANDUSE("ProjectMenu.landuse"),
		PROJECT_LANDUSE_LIST("ProjectMenu.landuse_list"),
		PROJECT_ELIGIBILITY("ProjectMenu.eligibility"),
		PROJECT_REMOVE_LU_ERROR("ProjectMenu.remove_lu_error"), 
		PROJECT_REMOVE_LU("ProjectMenu.remove_lu"), 
		//******
		PROJECT_ZONATION("ProjectMenu.zonation"),
		PROJECT_AREA("ProjectMenu.area"),
		PROJECT_AREA_SET("ProjectMenu.set_area"),
		PROJECT_ZONE_PARTITION("ProjectMenu.zone_partition"),
		PROJECT_ZONE("ProjectMenu.zone"),
		PROJECT_ZONE_AREA("ProjectMenu.zone_area"),
		PROJECT_ZONE_FRACTION("ProjectMenu.zone_fraction"),
		PROJECT_ZONE_ELIGIBILITY("ProjectMenu.zone_eligibility"),
		PROJECT_ZONE_DELETE("ProjectMenu.zone_delete"),
		PROJECT_ZONE_ERROR("ProjectMenu.zone_error"),
		PROJECT_ZONE_FRACTION_ERROR("ProjectMenu.zone_fraction_error"),
		PROJECT_ZONE_CHANGES("ProjectMenu.zone_changes"),
		PROJECT_AREA_ERROR("ProjectMenu.area_error"),
		PROJECT_ZONE_MATRIX_AREA_ERROR("ProjectMenu.zone_matrix_area_error"),
		PROJECT_ZONE_MATRIX_FRACTION_ERROR("ProjectMenu.zone_matrix_fraction_error"),
		PROJECT_ZONE_MATRIX_ZERO_ERROR("ProjectMenu.zone_matrix_zero_error"),
		
				PROJECT_LCOVER_MATRIX(
						"ProjectMenu.landcovermatrix"), 
						PROJECT_LCOVER_ZONE_MATRIX(
						"ProjectMenu.landcoverzonematrix"), 
						PROJECT_LCOVER_ZONE_CBU_MATRIX(
						"ProjectMenu.landcoverzonecbumatrix"),
				
				PROJECT_OUTPUT("ProjectMenu.output"),
				PROJECT_SPATIAL("ProjectMenu.spatial"),
				PROJECT_ECONOMIC("ProjectMenu.economic"),
				
		PROJECT_COSTB_UNIT("ProjectMenu.costbenefit_unit"),
		PROJECT_COSTB_UNIT_ADD("ProjectMenu.costbenefit_unit_add"),
		PROJECT_CBU_DELETE("ProjectMenu.cbu_delete"),
		PROJECT_CBU_DELETE_ERROR("ProjectMenu.cbu_delete_error"),
		
				PROJECT_EMISSION_ADD("ProjectMenu.emission_add"),
				
		PROJECT_AVOIDED_EMISSION("ProjectMenu.avoided_emission"),
		PROJECT_LU_FROM("ProjectMenu.from_lu"),
		PROJECT_LU_INTO("ProjectMenu.into_lu"),
		PROJECT_OPCOST("ProjectMenu.opcost"),
		PROJECT_ELIG_ONLY("ProjectMenu.eligible_only"),
		PROJECT_NET_EMISSION("ProjectMenu.net_emission"),
		PROJECT_TOTAL_EMISSION("ProjectMenu.total_emission"),
		PROJECT_TOTAL_SEQ("ProjectMenu.total_sequestration"),
		PROJECT_NET_CB("ProjectMenu.net_cb"),
		PROJECT_BENEFIT("ProjectMenu.benefit"),
		PROJECT_COST("ProjectMenu.cost"),
		PROJECT_NET_OPCOST("ProjectMenu.net_opcost"),
		PROJECT_POSITIVE_OPCOST("ProjectMenu.positive_opcost"),
		PROJECT_NEGATIVE_OPCOST("ProjectMenu.negative_opcost"),
		
				UNIT_EMISSION("Unit.emission"),
				UNIT_EMISSION_HA("Unit.emission_ha"),
				UNIT_CB("Unit.cb"),
				UNIT_CB_HA("Unit.cb_ha"),
				UNIT_OPCOST("Unit.opcost"),
				UNIT_FRAC_HA("Unit.fraction_ha"),
				UNIT_HECTARE("Unit.hectare"),
				UNIT_PROBABILITY("Unit.probability"),
				//******************
				
				PROJECT_COSTB("ProjectMenu.costbenefit"), 
				PROJECT_COSTB_TOTAL("ProjectMenu.costbenefit_total"), 
				PROJECT_NPV("ProjectMenu.npv"), 
				PROJECT_LCOVER_CHANGE("ProjectMenu.landcoverchange"),
				PROJECT_BG_EMISSION("ProjectMenu.belowground_emission"),
				PROJECT_MODIF_EMISSION("ProjectMenu.modified_emission"),
				PROJECT_SUMMARY("ProjectMenu.summary"), 
				PROJECT_SUMMARY_EMISSION("ProjectMenu.summary_emission"), 
//				PROJECT_SUMMARY_NPV("ProjectMenu.summary_npv"), 
				PROJECT_SUMMARY_OPCOST("ProjectMenu.summary_abatement"), 
				PROJECT_SUMMARY_ABATEMENT_CHART("ProjectMenu.summary_abatement_chart"), 
				PROJECT_SEQUESTRATION("ProjectMenu.sequestration"),
				PROJECT_ABATEMENT("ProjectMenu.abatement"),
				PROJECT_EMISSION("ProjectMenu.emission"),
				
				PROJECT_SUMMARY_TOTAL_EMISSION("ProjectMenu.summary_total_emission"),
				PROJECT_SUMMARY_TOTAL_SEQUESTRATION("ProjectMenu.summary_total_sequestration"),
				PROJECT_SUMMARY_TOTAL_EMISSION_ELIGIBLE("ProjectMenu.summary_total_emission_eligible"),
				PROJECT_SUMMARY_TOTAL_EMISSION_HA("ProjectMenu.summary_total_emission_ha"),
				PROJECT_SUMMARY_TOTAL_SEQUESTRATION_HA("ProjectMenu.summary_total_sequestration_ha"),
				PROJECT_SUMMARY_TOTAL_EMISSION_ELIGIBLE_HA("ProjectMenu.summary_total_emission_eligible_ha"),
				PROJECT_SUMMARY_TOTAL_SEQUESTRATION_ELIGIBLE("ProjectMenu.summary_total_sequestration_eligible"),
				PROJECT_SUMMARY_TOTAL_SEQUESTRATION_ELIGIBLE_HA("ProjectMenu.summary_total_sequestration_eligible_ha"),
				PROJECT_SUMMARY_TOTAL_COSTB_ELIGIBLE("ProjectMenu.summary_total_costb_eligible"),
				PROJECT_SUMMARY_TOTAL_COSTB_ELIGIBLE_HA("ProjectMenu.summary_total_costb_eligible_ha"),
				PROJECT_SUMMARY_TOTAL_COSTB("ProjectMenu.summary_total_costb"),
				PROJECT_SUMMARY_TOTAL_COSTB_HA("ProjectMenu.summary_total_costb_ha"),

				PROJECT_SUMMARY_NET_EMISSION("ProjectMenu.summary_net_emission"),
				PROJECT_SUMMARY_NET_EMISSION_ELIGIBLE("ProjectMenu.summary_net_emission_eligible"),
				PROJECT_SUMMARY_NET_EMISSION_HA("ProjectMenu.summary_net_emission_ha"),
				PROJECT_SUMMARY_NET_EMISSION_ELIGIBLE_HA("ProjectMenu.summary_net_emission_eligible_ha"),
				
				PROJECT_SUMMARY_THRESHOLD("ProjectMenu.summary_threshold"),
				PROJECT_SUMMARY_THRESHOLD_EMISSION("ProjectMenu.summary_threshold_emission"),
				
				

				MODEL_SIMULATION("ModelMenu.simulation"), 
				
				MODEL_SCENARIO("ModelMenu.scenario"),
				MODEL_MAIN("ModelMenu.main"),
				MODEL_ERROR("ModelMenu.model_error"),
				MODEL_WAIT("ModelMenu.wait"),
				
				MODEL_TPM("ModelMenu.tpm"),
				MODEL_LANDUSE_SCENARIO("ModelMenu.lu_scenario"), 
				MODEL_UNCONVERTED("ModelMenu.unconverted"),
				
				//**1.1.3
				MODEL_DYNAMIC("ModelMenu.dynamic"),
				MODEL_CSTOCK("ModelMenu.cstock"), 
				MODEL_NPV("ModelMenu.npv"),
				MODEL_CB("ModelMenu.costbenefit"),
				MODEL_BG("ModelMenu.belowground_emission"),
				MODEL_MODIF("ModelMenu.modified_emission"),
				//****
				
				MODEL_OUTPUT("ModelMenu.output"),
				MODEL_SUMMARY("ModelMenu.summary"),
				MODEL_CUMULATIVE("ModelMenu.cumulative"),
				
				MODEL_OUTPUT_MATRIX("ModelMenu.output_matrix"),
				MODEL_OUTPUT_CBU_MATRIX("ModelMenu.output_cbu_matrix"),
				
				MODEL_EMISSION("ModelMenu.emission"),
				MODEL_COSTB_TOTAL("ModelMenu.costbenefit_total"),
				MODEL_LANDUSE("ModelMenu.landuse"),
				MODEL_OPCOST("ModelMenu.opcost"),

				MODEL_CHECKOUT("ModelMenu.checkout"),
				MODEL_GENERATE("ModelMenu.generate"), 
				MODEL_REGENERATE("ModelMenu.regenerate"),
				MODEL_ITERASI("ModelMenu.iterasi"), 
				MODEL_PERIOD("ModelMenu.period"),
				MODEL_CHECKOUT_MSG("ModelMenu.checkout_msg"),
				MODEL_UPDATE_MSG("ModelMenu.update_msg"),
				MODEL_RUN("ModelMenu.run"),
				MODEL_PROCESS("ModelMenu.process"),
				
				MODEL_SCENARIO_UPDATE("ModelMenu.scenario_update_msg"),
				MODEL_SCENARIO_CHANGE("ModelMenu.scenario_change_msg"),
				MODEL_SCENARIO_BUTTON("ModelMenu.scenario_button_msg"),
				MODEL_SCENARIO_INVALID("ModelMenu.scenario_invalid_msg"),
				MODEL_SCENARIO_TPM_INVALID("ModelMenu.scenario_tpm_invalid_msg"),
				MODEL_SCENARIO_CHANGE_EARLY("ModelMenu.scenario_change_early_msg"),
				MODEL_SCENARIO_OVERWRITED("ModelMenu.scenario_overwrited_msg"),
				
				MODEL_ACTIVE("ModelMenu.active"),
				MODEL_INVALID_VALUE("ModelMenu.invalid_value"),
				MODEL_INVALID_ROW("ModelMenu.invalid_row"),
				MODEL("ModelMenu.model") 
				;

		private final ImageIcon icon16x16, icon24x24, icon32x32, icon48x48;
		private final String label, description;
		private final char mnemonic;

		MenuProperty(String key) {
			label = getString(key + "_label");
			description = getString(key + "_accessible_description");
			String m = getString(key + "_mnemonic");
			if ((m == null) || (m.equals(""))) {
				mnemonic = '\u0000';
			} else {
				mnemonic = m.charAt(0);
			}
			String imagePath = "/icraf/carbon/gui/images/";
			icon16x16 = getImage(imagePath + "16x16/" + key + ".png");
			icon24x24 = getImage(imagePath + "24x24/" + key + ".png");
			icon32x32 = getImage(imagePath + "32x32/" + key + ".png");
			icon48x48 = getImage(imagePath + "48x48/" + key + ".png");
		}

		public String getAccessibleDescription() {
			return description;
		}

		public String getActionCommand() {
			return toString();
		}

		public ImageIcon getIcon(IconModel iconModel) {
			if (iconModel == IconModel.ICON16) {
				return icon16x16;
			} else if (iconModel == IconModel.ICON24) {
				return icon24x24;
			} else if (iconModel == IconModel.ICON32) {
				return icon32x32;	
			} else if (iconModel == IconModel.ICON48) {
				return icon48x48;
			}
			return null;
		}

		public String getLabel() {
			return label;
		}

		public char getMnemonic() {
			return mnemonic;
		}

		public boolean isAction(ActionEvent e) {
			return e.getActionCommand().equals(getActionCommand());
		}

	}

	static String bundelName = "abacus_menu";
	static ResourceBundle bundle;
	public static Locale locale;

	public static ImageIcon getAbacusBanner() {
		String imagePath = "/icraf/carbon/gui/images/abacus_banner.png";
		return getImage(imagePath);
	}
	
	public static ImageIcon getIcrafImage() {
		String imagePath = "/icraf/carbon/gui/images/icraf_logo.png";
		return getImage(imagePath);
	}
	
	public static ImageIcon getEUImage() {
		String imagePath = "/icraf/carbon/gui/images/eu_logo.png";
		return getImage(imagePath);
	}
	
	public static ImageIcon getAllreddiImage() {
		String imagePath = "/icraf/carbon/gui/images/allreddi_logo.png";
		return getImage(imagePath);
	}
	

	/**
	 * Returns the resource bundle
	 */
	public static ResourceBundle getResourceBundle() {
		if (bundle == null) {
			if(locale == null) {
				bundle = ResourceBundle.getBundle(bundelName, new UTF8Control());
			} else {
				bundle = ResourceBundle.getBundle(bundelName, locale, new UTF8Control());
			}
		}
		return bundle;
	}

	/**
	 * This method returns a string from the resource bundle.
	 */
	protected static String getString(String key) {
		String value = null;
		try {
			value = getResourceBundle().getString(key);
		} catch (MissingResourceException e) {
			System.out.println("[MissingResources] Couldn't find value for: "
					+ key);
		}
		return value;
	}

	public static ImageIcon getImage(String file) {
        URL u = MenuFactory.class.getResource(file);
		if (u == null)
			return null;
		return new ImageIcon(u);
	}


	public static void setLanguage(String languageID) {
		ResourceBundle.clearCache();
		Locale loc = new Locale(languageID);
		locale = loc;
		bundle=null;
	}

}
